package cn.tedu.kcircle.check.admin.adapter.http;

import cn.tedu.dto.ContentUpdateCheckStateDTO;
import cn.tedu.kcircle.check.admin.adapter.converter.CheckConverter;
import cn.tedu.kcircle.check.admin.protocol.param.CheckLogAddParam;
import cn.tedu.kcircle.check.admin.repository.model.CheckLogAddModel;
import cn.tedu.kcircle.check.admin.repository.persist.CheckLogRepository;
import cn.tedu.kcircle.check.admin.service.CheckAdminService;
import cn.tedu.kcircle.check.admin.service.bo.CheckStateUpdateBO;
import cn.tedu.kcircle.common.dto.ResourceDTO;
import cn.tedu.kcircle.common.po.CurrentPrincipal;
import cn.tedu.kcircle.content.admin.api.rpc.ContentAdminRpcApi;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
@Slf4j
public class CheckAdminServiceImpl implements CheckAdminService {
    private CheckLogRepository checkLogRepository;
    private ContentAdminRpcApi contentAdminRpcApi;
    private CheckConverter converter;

    private void updateCheckStateById(CheckStateUpdateBO checkStateUpdateBO, Integer checkState) {
        Integer resourceType = checkStateUpdateBO.getResourceType();
        int fromCheckState = contentAdminRpcApi.queryCheckStateById(checkStateUpdateBO.getResourceId());
        int checkSuccess=0;
        if (isResourceContent(resourceType)) {
            ContentUpdateCheckStateDTO contentUpdateCheckStateDTO = converter.bo2ContentDTO(checkStateUpdateBO);
            contentUpdateCheckStateDTO.setFromCheckState(fromCheckState);
            contentUpdateCheckStateDTO.setToCheckState(checkState);
            checkSuccess=contentAdminRpcApi.updateCheckStateById(contentUpdateCheckStateDTO);
        }
        //TODO 审核评论
        if(checkSuccess>0){
            //记录日志
            CheckLogAddModel checkLogAddModel = new CheckLogAddModel();
            checkLogAddModel.setCheckUserId(checkStateUpdateBO.getOpUserId())
                    .setCheckUsername(checkStateUpdateBO.getOpUsername())
                    .setFromCheckState(fromCheckState)
                    .setToCheckState(checkState)
                    .setCheckRemarks(checkLogAddModel.getCheckRemarks());
            checkLogRepository.insert(checkLogAddModel);
        }
    }

    @Override
    public void checkPass(CheckStateUpdateBO checkStateUpdateBO) {
        log.debug("开始处理【审核通过】的业务，参数：{}", checkStateUpdateBO);
        updateCheckStateById(checkStateUpdateBO,CHECK_STATE_PASS);
    }

    @Override
    public void checkReject(CheckStateUpdateBO checkStateUpdateBO) {
        log.debug("开始处理【审核拒绝】的业务，参数：{}", checkStateUpdateBO);
        updateCheckStateById(checkStateUpdateBO,CHECK_STATE_REJECT);
    }

    private boolean isResourceContent(Integer resourceType) {
        return resourceType == RESOURCE_TYPE_QUESTION || resourceType == RESOURCE_TYPE_ARTICLE || resourceType == RESOURCE_TYPE_ANSWER || resourceType == RESOURCE_TYPE_VIDEO || resourceType == RESOURCE_TYPE_IDEA;
    }

    private boolean isResourceComment(Integer resourceType) {
        return resourceType == RESOURCE_TYPE_COMMENT;
    }
}
